package com.cindata

import com.cindata.dictionary.ExecStatusDict
import grails.converters.JSON
import org.springframework.security.access.annotation.Secured

import grails.transaction.Transactional

@Secured(['permitAll'])
@Transactional
class ApiController
{
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def apiService

    def index()
    {
    }

    /**
     * 评估结果回调
     **/
    def evaluateCallBack()
    {
        //接收参数
        def params = readReqJSONParams(request.getReader())
        def evaluateCode = params?.evaluateCode
        def batchEstate = BatchEstate.findByEvaluateCode(evaluateCode)
        def estateImport = EstateImport.findByEvaluateCode(evaluateCode)
        if (!estateImport && !batchEstate)
        {
            render([message: "被评估的押品不存在", code: '1'] as JSON)
            return
        }

        // 重估回调
        if (batchEstate)
        {
            def estate = batchEstate?.estate
            // 导入回调
            if (params?.saleprice == 0)
            {
                //评估失败
                batchEstate.evaluateStatue = ExecStatusDict.FAILURE()
                batchEstate.save(flush: true)
            }
            else
            {
                //评估成功
                estate.modifiedDate = new Date()
                estate.unitPrice = params.unitprice
                estate.totalPrice = params.saleprice
                estate.latestEvaluatePrice = params.latestEvaluatePrice
                estate.latestEvaluateDate = Date.parse("yyyy-MM-dd H:m:s", params.evaluateDate)
                estate.save(flush: true)

                batchEstate.evaluateStatue = ExecStatusDict.SUCCESS()
                batchEstate.save(flush: true)
            }
        }
        else
        {
            // 导入回调
            if (params?.saleprice == 0)
            {
                //评估失败
                estateImport.communityId = params?.communityId as Long
                estateImport.communityName = params?.info?.name
                estateImport.communityAddress = params?.info?.address
                estateImport.unitPrice = params?.unitprice
                estateImport.evaluateDate = Date.parse("yyyy-MM-dd H:m:s", params?.evaluateDate)
                estateImport.failReason = params?.failReason
                estateImport.evaluateStatue = ExecStatusDict.FAILURE()
                estateImport.save(flush: true)
                estateImport.evaluteFailure()
            }
            else
            {
                //评估成功
                estateImport.communityId = params?.communityId as Long
                estateImport.communityName = params?.info?.name
                estateImport.communityAddress = params?.info?.address
                estateImport.buildingId = params?.buildingId as Long
                estateImport.unitId = params?.unitId as Long
                estateImport.houseId = params?.houseId as Long
                estateImport.evaluateCode = params?.evaluateCode
                estateImport.evaluateStatue = ExecStatusDict.SUCCESS()
                estateImport.unitPrice = params?.unitprice
                estateImport.totalPrice = params?.saleprice
                estateImport.evaluateDate = Date.parse("yyyy-MM-dd H:m:s", params?.evaluateDate)
                estateImport.buildYear = params?.info?.buildYear
                estateImport.totalBuildingNum = params?.info?.totalBuildingNum
                estateImport.totalHouseNum = params?.info?.totalHouseNum
                if (params?.bound)
                {
                    def zb = params?.bound.split(',')
                    estateImport.x = zb[0] as double
                    estateImport.y = zb[1] as double
                }
                else
                {
                    //没有坐标先用小区坐标代替
                    if (estateImport.communityId)
                    {
                        //小区详情
                        def community = apiService.community(estateImport?.city?.zoneNumber, estateImport.communityId)
                        if (community.data)
                        {
                            estateImport.x = community?.data?.x as double
                            estateImport.y = community?.data?.y as double
                        }
                    }
                }
                estateImport.save(flush: true)
                estateImport.evaluteSuccess()
            }
        }

        render([message: "成功", code: '0'] as JSON)
    }

    // 读取请求的JSON参数
    private def readReqJSONParams(BufferedReader reader)
    {
        String line = null
        StringBuffer json = new StringBuffer()
        while ((line = reader.readLine()) != null)
        {
            json.append(line)
        }
        reader.close()
        def params = JSON.parse(json.toString())
        log.info("---------------->CallBack------------->json result is: ${params.inspect()}")
        return params
    }

}
